
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>人工发送短信</title>
<link href="../../layui/css/iconfont/iconfont.css" rel="stylesheet"
	type="text/css">
<link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
<link href="../../css/jquery-ui-1.10.4.custom.css" rel="stylesheet"
	type="text/css">
<link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet"
	type="text/css">
<script src="../../layui/layui.js"></script>
<script src="../../js/jquery-1.10.2.js"></script>
<script src="../../js/jquery-ui-1.10.4.custom.js"></script>
<script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
<script src="../../js/util.js"></script>
<script>

	function back() {
		location.href = "smsArtificialRecordManage.html";
	}
	layui.use([ 'form', 'jquery', 'element', 'laydate', 'layer', 'upload' ], function() {
		var form = layui.form;
		var $ = layui.$;
		var laydate = layui.laydate;
		var layer = layui.layer;
		var upload = layui.upload;
		var batchNumber; //批次号
		laydate.render({
			elem : '#sendTime',
			type : 'datetime',
			value : new Date()
		});
		$('#upload2').on('click', function() {
			var choose = $("span.layui-upload-choose").text();
			if (choose != null && choose != '') {
				$('#upload').trigger("click");
			} else {
				var cityId = $("#cityId").val();
				var mgrId = $('#mgrId').val();
				var mgrNameId = $('#mgrName_id').val();
				var remark = $('#remark').val();
				var sendTime = $('#sendTime').val();
				if (isNull(cityId)) {
					layer.msg("您必须选择一个地市");
					return false;
				} else if ((isNull(mgrId)) && (isNull(mgrNameId))) {
					layer.msg("您必须选择一个项目名称或者项目简称");
					return false;
				} else if (isNull(remark)) {
					layer.msg("请添加发送理由");
					return false;
				} else if (remark.length > 100) {

					layer.msg("发送理由超出长度限制");
					return false;
				} else {
					layer.msg('请导入号码!'
					)
				}
			}
		});
		upload.render({
			elem : '#import',
			url : '/artificialRecord/upload',
			//url : '/upload/templateFile',
			auto : false, //关闭自动上传
			accept : 'file', //普通文件
			exts : 'csv', //只允许上传csv文件 //,multiple: true
			//exts : 'pdf', //只允许上传csv文件 //,multiple: true
			bindAction : '#upload',
			before : function(obj) {
				var mgrId = $('#mgrId').val().trim();
				var mgrName_id = $('#mgrName_id').val();
				var mgrName = $('#mgrName').val().trim();
				var joinFrequencyRoom = $("input[name='joinFrequencyRoom']:checked").val();
				this.data = {
					cityId : $('#cityId').val(),
					countyId : $('#countyId').val(),
					mgrId : $('#mgrId').val(),
					mgrNameId : $('#mgrName_id').val(),
					remark : $('#remark').val(),
					sendTime : $('#sendTime').val(),
					joinFrequencyRoom : joinFrequencyRoom,
					batchNumber : batchNumber
				};
				layer.load(); //上传loading
			},
			done : function(obj) {
				layer.closeAll('loading'); //关闭loading
				if (obj.code == 0) {
					var data = obj.data;
					var num = data.toString().split(",");
					layer.confirm('文件上传成功，确认返回人工发送记录查询界面查看结果', {
						btn : [ '确认', '取消' ] //按钮
					}, function() {
						location.href = "smsArtificialRecordManage.html";
					}, function() {
						location.href = "smsArtificialRecordImport.html";
					});

				} else {
					layer.msg(obj.msg)
				}
			},
			error : function(index, upload) {
				layer.closeAll('loading'); //关闭loading
				layer.msg("上传过程中出现异常，请重试");
			}
		});


		$('#mgrName').bind('input propertychange', function() {
			//获取input 元素,并实时监听用户输入
			$("#mgrName_id").val("");
		});
		var jsonData = [];
		$("#mgrName").keyup(function() {

			var key = $("#mgrName").val();
			var cityId = $("#cityId").val();
			var countyId = $("#countyId").val();
			var url = '/util/selectMgrNameLike';
			var param = {
				cityId : cityId,
				countyId : countyId,
				key : key
			};
			$.ajax({
				type : 'post',
				async : false,
				url : url,
				data : param,
				success : function(json) {
					jsonData = json.data;
				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			}
			);
		});
		//联想
		$("#mgrName").autocomplete({
			source : function(request, response) {
				var a = [];
				jsonData.forEach(function(item) {
					a.push({
						// 设置item信息
						label : item.name, // 下拉项显示内容
						value : item.name, // 下拉项对应数值
						id : item.id, //自定义字段
					});
				});
				response(a);
			},
			minLength : 2, // 输入框字符个等于2时开始查询
			select : function(event, ui) { // 选中某项时执行的操作
				//存放选中选项的信息
				$("#mgrName_id").val(ui.item.id);
				$("#mgrName").val(ui.item.name);

				var url = '/content/findContentByMgrId';
				var param = {
					mgrId : ui.item.id
				};
				$.ajax({
					type : 'post',
					async : false,
					url : url,
					data : param,
					beforeSend : function() {
						loading = layer.load(0, {
							shade : false
						});
					},
					success : function(json) {
						layer.close(loading);
						if (json.code == 0) {
							var data = json.data;
							if (!isNull(data.newContent)) {
								$("#content").val(data.newContent);
							} else {
								if (!isNull(data.content)) {
									$("#content").val(data.content);
								}
							}
							form.render();
						} else {
							//layer.msg(json.msg);
							layer.confirm('该项目无内容，请前往配置', {
								btn : [ '确认', '取消' ] //按钮
							}, function() {
								location.href = "../contentManage/smsContentConfigure.html?mgrId=" + mgrId + "&cityId=" + cityId;
							}, function() {
								location.href = "smsArtificialRecordManage.html";
							});
						}
					},
					error : function(data) {
						layer.msg('系统异常，请联系管理员处理!');
					}
				});
			}
		});


		//身份识别+初始化编码
		$(document).ready(function() {
			$.ajax({
				type : 'post',
				async : false,
				url : '/artificialRecord/initBatchNumber',
				success : function(json) {
					if (json.code == 0) {
						batchNumber = json.data;
						$("#batchNumber").html("批次号:" + batchNumber);
					} else {
						layer.msg('系统异常，请联系管理员处理!');
					}
				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			});

			$.ajax({
				type : 'post',
				async : false,
				url : '/util/findUser',
				success : function(json) {
					user = json;
					if (json.duty == 1) { //地市
						$("#cityId").val(json.cityId);
						$("#cityId").attr("disabled", true);
						var data3 = selectMgrInfo(json.cityId);
						appendMgr(data3);
						var data2 = selectCountyId(json.cityId);
						appendCounty(data2);
						form.render();
					} else if (json.duty == 2) { //区县
						$("#cityId").val(json.cityId);
						$("#cityId").attr("disabled", true);
						var data3 = selectMgrInfo(json.countyId);
						appendMgr(data3);
						var data2 = selectCountyId(json.cityId);
						appendCounty(data2);
						$("#countyId").val(json.countyId);
						$("#countyId").attr("disabled", true);
						form.render();                   
					} else if (json.duty == 3) { //项目
						appendMgr(json.list);
						var data3 = selectMgrInfo(json.countyId);
						appendMgr(data3);
						var data2 = selectCountyId(json.cityId);
						appendCounty(data2);
						$("#cityId").val(json.cityId);
						$("#cityId").attr("disabled", true);
						$("#countyId").val(json.countyId);
						$("#countyId").attr("disabled", true);
						$.ajax({
							type : 'post',
							async : false,
							url : '/util/getUserMgrName',
							success : function(json) {
								data = json.data;
								appendMgr(data)
							}
						})
						form.render();
					}
				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			});
		});

		//监听区县
		form.on('select(cityId)', function(data) {
			var data2 = selectCountyId(data.value);
			appendCounty(data2);
			var data1 = selectMgrInfo(data.value);
			appendMgr(data1);
		});
		//根据城市ID查询区县
		function selectCountyId(cityId) {
			var data;
			var url = '/util/findCountyByCityId?cityId=' + cityId;
			$.ajax({
				type : 'post',
				async : false,
				url : url,
				success : function(json) {
					data = json.data;
				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			}
			);
			return data;
		}
		//动态拼接区县
		function appendCounty(data) {
			if (data.length != null) {
				var options = '<option value="">请先选择地市</option>';
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
				}
				$('#countyId').html(options);
			} else {
				$('#countyId').html('该地市下没有区县');
			}
			form.render();
		}

		//监听项目
		form.on('select(countyId)', function(data) {
			var data3;
			if (null != data.value && "" != data.value) {
				data3 = selectMgrInfo(data.value);
			} else {
				var cityId = $("#cityId").val();
				data3 = selectMgrInfo(cityId);
			}
			appendMgr(data3);
		});
		//根据城市ID查询项目
		function selectMgrInfo(cityId) {
			var data;
			var url = '/util/findByCityId?cityId=' + cityId;
			$.ajax({
				type : 'post',
				async : false,
				url : url,
				success : function(json) {
					data = json.data;
				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			}
			);
			return data;
		}

		//根据城市ID和区县查询项目
		function selectMgrInfo(countyId) {
			var data;
			var url = '/util/findByMgrId?countyId=' + countyId;
			$.ajax({
				type : 'post',
				async : false,
				url : url,
				success : function(json) {
					data = json.data;
				},
				error : function() {
					layer.msg('系统异常，请联系管理员处理!');
				}
			}
			);
			return data;
		}
		//动态拼接项目
		function appendMgr(data) {
			if (data.length != null) {
				var options = '<option value="">请选择</option>';
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
				}
				$('#mgrId').html(options);
			} else {
				$('#mgrId').html('<option value="">该区县下没有项目</option>');
			}
			form.render();
		}

		//监听短信内容
		$('textarea').bind('input propertychange', function() {
			var contentLength = $("#remark").val().length;
			$("#contentSize").html(contentLength);
		});
		form.on('select(mgrId)', function(mgrId) {
			var mgrId = $("#mgrId").val();
			var cityId = $("#cityId").val();
			if (mgrId == "") {
				layer.msg('请选择一个项目');
			} else {
				var url = '/content/findContentByMgrId';
				var param = {
					mgrId : mgrId
				};
				$.ajax({
					type : 'post',
					async : false,
					url : url,
					data : param,
					beforeSend : function() {
						loading = layer.load(0, {
							shade : false
						});
					},
					success : function(json) {
						layer.close(loading);
						if (json.code == 0) {
							var data = json.data;
							if (!isNull(data.content)) {
								$("#content").val(data.content);
							}
							form.render();
						} else {
							layer.confirm('该项目无内容，请前往配置', {
								btn : [ '确认', '取消' ] //按钮
							}, function() {
								location.href = "../contentManage/smsContentConfigure.html?mgrId=" + mgrId + "&cityId=" + cityId;
							}, function() {
								location.href = "smsArtificialRecordManage.html";
							});
						}
					},
					error : function(data) {
						layer.msg('系统异常，请联系管理员处理!');
					}
				});
			}
		});

	});
</script>
</head>
<body>

	<!--container-->
	<div class="layui-content-body">
		<div class="scroll-box">
			<div class="breadcrumb-box">
				<span class="layui-breadcrumb" lay-separator="|"> <a
					href="javascript:returnIndex2()">首页&nbsp;</a> <a href="">
						&nbsp;短信发送管理&nbsp; </a> <a href=""> &nbsp;人工发送短信&nbsp; </a>
				</span>
			</div>
			<div class="content-box">
				<h4 class="tips-h4">
					<i class="iconfont icon-xinxi"></i>
					<div id="batchNumber" class="layui-inline"></div>
				</h4>
				<div class="layui-form layui-form-pane">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">地市</label>
							<div class="layui-input-block" style="width: 150px;">
								<select name="cityId" lay-filter="cityId" id="cityId">
									<option value="">请选择</option>
									<option value="11">苏州</option>
									<option value="12">淮安</option>
									<option value="13">宿迁</option>
									<option value="14">南京</option>
									<option value="15">连云港</option>
									<option value="16">徐州</option>
									<option value="17">常州</option>
									<option value="18">镇江</option>
									<option value="19">无锡</option>
									<option value="20">南通</option>
									<option value="21">泰州</option>
									<option value="22">盐城</option>
									<option value="23">扬州</option>
									<option value="25">省级</option>
								</select>
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">区县</label>
							<div class="layui-input-block" style="width:150px">
								<select name="countyId" lay-filter="countyId" id="countyId">
									<option value="">全部</option>
								</select>
							</div>
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">项目简称</label>
							<div class="layui-input-block" style="width:200px">
								<input type="text" name="mgrName" id="mgrName"
									lay-verify="required" value="" autocomplete="on"
									class="layui-input" /> <input type="hidden" id="mgrName_id" />
							</div>
						</div>
						<div class="layui-inline" style="color: red">项目简称与项目名称只需选择一个即可
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">项目名称</label>
							<div class="layui-input-block" style="width:200px">
								<select name="mgrId" lay-filter="mgrId" id="mgrId">
									<option value="">请先选择一个项目</option>
								</select>
							</div>
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">发送内容</label>
						<div class="layui-input-block">
							<textarea class="layui-textarea" disabled id="content"
								style="width:32%"></textarea>
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">发送时间</label>
							<div class="layui-input-inline" style="width:200px">
								<input type="text" class="layui-input" id="sendTime"
									placeholder="请选择">
							</div>
						</div>
					</div>
					<div class="layui-form-item" id="joinFrequencyRoom">
						<div class="layui-block" pane="">
							<label class="layui-form-label" style="width: auto">号码是否加入频次库</label>
							   <div class="layui-input-block">
								<input type="radio" name="joinFrequencyRoom" value="1" title="是"> 
								<input type="radio" name="joinFrequencyRoom" value="0" title="否" checked="checked">
								<span style="color:red;">*</span>
							</div>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">发送理由</label>
						<div class="layui-input-block">
							<textarea placeholder="请输入内容" class="layui-textarea"
								style="width:32%" id="remark"></textarea>
						</div>
					</div>
					<div class="layui-form-item">
						<h4 class="tips-h4">
							<i class="iconfont icon-xinxi"></i> 理由长度<span id="contentSize">0</span>个字，限制100字
						</h4>
					</div>

					<div class="layui-form-item" style="display: flex;">
						<button type="button" class="layui-btn" id="import">
							<i class="layui-icon"></i>发送号码导入
						</button>
						<span><a href="/artificialRecord/download">（人工发送模板下载）</a></span>
					</div>
					
					<div class="layui-form-item" style="display:none;">
						<button class="layui-btn" id="upload">提交</button>
					</div>
					<div class="layui-form-item">
						<button class="layui-btn layui-btn-normal" id="upload2">上传</button>
						<button class="layui-btn layui-btn-normal" type="button"
							onclick="back()">返回</button>
						</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	</div>
</body>
</html>